﻿Imports System.Text
Imports QBuilder

<TestClass()>
Public Class ComplexQueryTest

    <TestMethod()>
    Public Sub ComplexQuery1()

        Dim Qry As New QBuilder.SelectQuery

        Qry.TableName = "test"
        Qry.Fields.Add("a")
        Qry.Fields.Add("b")
        Qry.Fields.Add("c")

        Qry.AddCriteria(New QBuilder.Criteria("a", Operation.EqualTo, "@Param1"))
        Qry.AddCriteria(New QBuilder.Criteria("b", Operation.EqualTo, "Value"))
        Qry.AddCriteria(New QBuilder.BetweenCriteria("c", "56", "@Param2"))

        Dim subquery As New QBuilder.SelectQuery
        subquery.TableName = "SubTable"
        subquery.Fields.Add("count(*)")
        subquery.AddCriteria(New QBuilder.Criteria("Sub1", Operation.EqualTo, "15"))

        Qry.AddCriteria(New QBuilder.Criteria(subquery, Operation.EqualTo, "2"))

        Dim expected As String = "SELECT a, b, c FROM test WHERE a = @Param1 AND b = 'Value' AND c BETWEEN 56 AND @Param2 AND (SELECT count(*) FROM SubTable WHERE Sub1 = 15) = 2"

        Assert.AreEqual(expected, Qry.GetSQLText)
    End Sub

End Class
